// pages/school/index.js
const App = getApp()
const utils = require("../../../utils/util.js")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: "我发布的招聘信息",
        window: App.globalData.window,
        bodyBgColor: "#f0f0f0",
        // 本地缓存的用户信息
        loadMore: {
            show: false,
            type: "loading",
            text: "加载中..."
        },
        theamTextColor: App.globalData.theamTextColor,
        list: [
        ],
        page: {
            pageNum: 1,
            pageSize: 15,
            pages: 0
        }
    },
    URL: {
        pageList: '/partTimeJob/myPublish',
        delete: '/partTimeJob/deleteInfoByCode/?'
    },
    page: {
        userInfoCenter: '/pages/tabbar/user/userInfoCenter/index',
        home: '/pages/tabbar/index/index',
        schoolPage: "/pages/school/index",
        shareTarget: '/pages/recruit/share/index',
        edit: '/pages/recruit/add/index',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        App.$handlerInviteCode(options);
        this.inInit();
    },

    toEdit(e) {
        let jobCode = utils.getEvent(e).item.jobCode;
        App.$navigateTo(this.page.edit + "?jobCode=" + jobCode, true)
    },

    delete(e) {
        let that = this;
        let options = {
            msg: "是否确认删除?",
            success: function () {
                let item = utils.getEvent(e).item;
                let index = utils.getEvent(e).index;
                App.$delete(true, that.URL.delete.replace("?", item.jobCode), {}, res => {
                    if (res.R) {
                        let list = that.data.list;
                        list.splice(index, 1);
                        that.setData({
                            "list": list
                        })
                    }
                })
            }
        };
        App.$alertConfirm(options)
    },

    inInit() {
        this.init(this.data.page.one, false);
    }
    // 初始化数据
    , init(pageNum, loadMore) {
        let item = this.data.searchParams;

        let params = {};
        for (const itemKey in item) {
            params[itemKey] = item[itemKey];
        }
        params['pageNum'] = pageNum
        params['pageSize'] = this.data.page.pageSize

        // 上拉到底的时候进行显示加载中
        this.setLoadingMore(loadMore, "loading", "加载中...")

        if (!loadMore ? true : loadMore) {
            // 弹出loading
            wx.showLoading({
                title: '加载中',
                mask: true,
            })
        }

        App.$get(!loadMore ? true : loadMore, this.URL.pageList, params, res => {
            wx.stopPullDownRefresh();
            this.setData({
                "loadMore.show": false,
            })
            if (res.R) {
                this.setData({
                    'page.pageNum': res.data.pageNum,
                    'page.pageSize': res.data.pageSize,
                    'page.pages': res.data.pages
                })

                let newData = res.data.rows;
                let sourceData = this.data.list;
                if (!loadMore) {
                    sourceData = newData;
                } else {
                    // 上啦刷新，列表追加数据
                    newData.forEach(data => {
                        sourceData.push(data);
                    })
                }
                this.setData({
                    list: sourceData,
                })
            }

            if (!loadMore ? true : loadMore) {
                wx.hideLoading();
            }
        })
    }

    , setLoadingMore(show, type, text) {
        this.setData({
            "loadMore.show": show,
            "loadMore.type": type,
            "loadMore.text": text,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    goBack() {
        App.$switchTab(this.page.userInfoCenter, false)
    },


    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.init(1, false);
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        let page = this.data.page;
        if (page.pages === 0 || page.pageNum + 1 > page.pages) {
            this.setLoadingMore(true, "end", "我是有底线的~")
            return;
        }
        this.init(page.pageNum + 1, true);
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (e) {
        let item = null;
        if (e.from !== "menu") {
            item = e.target.dataset.detail
        }
        let str = JSON.stringify(item);
        return App.$onShare(this.page.shareTarget, "&item=" + encodeURIComponent(str), item.title);
    }
})
